var index_books_order = 1;

$(document).ready(function (){
	view_init();
	event_init();
});

function view_init()
{
	loadingStart();
	
	$("[data-type=dateAll]").each(function (){
		$(this).datetimepicker({
			  timeFormat: "HH:mm:ss",
	          dateFormat: "yy-mm-dd",
			  monthNames: [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ]
		});
	});
	
	list_list(true);
	
	$("#main-content").show();
}

function event_init()
{
	// 查询按钮
	$("#query").click(function (){
//		var createTimeStringStart = $("[data-id=createTimeStringStart]").val();
//		var createTimeStringEnd = $("[data-id=createTimeStringEnd]").val();
//		if (createTimeStringStart && createTimeStringEnd)
//		{
//			if (moment(createTimeStringStart).isAfter(createTimeStringEnd))
//			{
//				$("body").umDialog("alert" ,{"content":"开始时间不能大于结束时间"});
//				return false;
//			}
//		}
		$("#list_tbody").html("");
		list_list(true);
	});
	
	// 新增按钮
	$("#add").click(function (){
		window.location.href = "/normalbooks/direction/edit.do";
	});
	
	
	// 编辑按钮
	$("#list_tbody").delegate("[data-id=edit]" ,"click" ,function (){
		window.location.href = "/normalbooks/direction/edit.do?id="+$(this).parent().attr("data-id");
	});
	
	// 详情按钮
	$("#list_tbody").delegate("[data-id=detail]" ,"click" ,function (){
		showBook($(this).parent());
	});
	
	// 删除按钮
	$("#list_tbody").delegate("[data-id=delete]" ,"click" ,function (){
		alert("暂时未开放此功能");
	});
	
	// 翻页事件
	index_initPageEvent("list_list()");
	
	// 图书tab展示点击事件
    $("#tab-ul").delegate("li" ,"click" ,function (){
  	  $("#tab-ul").find("li").removeClass("tab-ul-selected");
  	  $(this).addClass("tab-ul-selected");
	  $("#main-content").children("div").hide();
	  $("#main-content").children("div[id='book"+$(this).attr("data-id")+"']").show();
    });
    
    // 图书tab展示删除事件
    $("#tab-ul").delegate("[data-id=tab_close]" ,"click" ,function (){
  	  index_deleteTab(this);
  	  return false;
    });
}

function list_list(isFirst)
{
	if (isFirst)
	{
		index_pageObj.init();
	}
	
	loadingStart();
	
	var obj = $("#query_panel").umDataBind("serialize" ,"");
	obj.currentPage = index_pageObj.currentPage;
	obj.pageSize = index_pageObj.pageSize;
	obj.bookType = 2;//普通图书
	if(obj.id == "")
		obj.id = -1;
	um_ajax_post(
			INDEX_WEBAPP + "/childrenbooks/select.do",
			obj,
			function (data){
				$("#page_record_count").text(data.size);
				_render(data.childrenbooks);
			},
			null);
	function _render(data)
	{
		var buffer = [];
		if (data && data.length >0)
		{
			for (var i=0;i<data.length;i++)
			{
				buffer.push('<tr data-id="bookDetail" >');
				buffer.push('<td id="book_id" >'+data[i].id+'</td>');
				buffer.push('<td id="book_title_short">'+shortTitle(data[i].title)+'</td>');
				buffer.push('<td id="book_title" style="display:none">'+beautyNull(data[i].title)+'</td>');
				buffer.push('<td id="book_bookrecno">'+beautyNull(data[i].bookrecno)+'</td>');
				buffer.push('<td id="book_isbn">'+beautyNull(data[i].isbn)+'</td>');
				buffer.push('<td id="book_publisher">'+beautyNull(data[i].publisher)+'</td>');
				buffer.push('<td style="display:none">'+beautyNull(data[i].bookImageUrl)+'</td>');
				buffer.push('<td style="text-align: center" data-id="'+data[i].id+'"><a href="#" data-id=edit>编辑 |</a><a href="#" data-id=detail> 详情 |</a><a href="#" data-id=delete> 删除</a></td>');
				buffer.push('<tr>');
			}
			$("#list_tbody").html(buffer.join(""));
		}
		else
		{
			index_pageObj.toEndPage();
		}

		index_pageObj.isEndMethod(data);

	}
}
/**
 * 图书展示添加tab
 * */
function index_addTab(data)
{
	$("#tab-ul").find("li").removeClass("tab-ul-selected");
	$("#book0").hide();
	var buffer = [];
	buffer.push('<li class="tab-ul-selected" data-id="'+index_books_order+'">');
//	buffer.push('<span>'+$(liObj).find("span").html()+'</span>');\
	var bookName = $(data).parent().children("td").eq(2).text();
	buffer.push('<span>'+bookName>6?bookName.substr(0,6) + "..." :bookName +'</span>');
	buffer.push('<div class="tab_close" data-id="tab_close"></div>');
	buffer.push('</li>');
	$("#tab-ul").append(buffer.join(""));
	//创建图书展示div
	var bookDiv = $('<div id="book'+index_books_order+'" class="container"></div>');
	var detailBuffer = [];
//	detailBuffer.push('<div class="form-group">');
//	detailBuffer.push('<label class="control-label col-1of10">test</label>');
//	detailBuffer.push('<label class="control-label col-1of3" data-id="title">'+$(data).parent().children("td[id='book_title']").text()+'</label>');
//	detailBuffer.push('</div>');
	
	detailBuffer.push('<div class="panel">');
	detailBuffer.push('<div class="panel-content" style="display:block">');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<label class="control-label col-1of10">书名</label>');
	detailBuffer.push('<label class="control-label col-1of3">'+$(data).parent().children("td[id='book_title']").text()+'</label>');
	detailBuffer.push('</div>');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<label class="control-label col-1of10">作者</label>');
	detailBuffer.push('<label class="control-label col-1of3">'+$(data).parent().children("td[id='book_author']").text()+'</label>');
	detailBuffer.push('</div>');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<label class="control-label col-1of10">图书类型</label>');
	detailBuffer.push('<label class="control-label col-1of3">'+$(data).parent().children("td[id='book_bookType']").text()+'</label>');
	detailBuffer.push('</div>');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<label class="control-label col-1of10">图书记录号</label>');
	detailBuffer.push('<label class="control-label col-1of3">'+$(data).parent().children("td[id='book_bookrecno']").text()+'</label>');
	detailBuffer.push('</div>');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<label class="control-label col-1of10">classno</label>');
	detailBuffer.push('<label class="control-label col-1of3">'+$(data).parent().children("td[id='book_classNo']").text()+'</label>');
	detailBuffer.push('</div>');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<label class="control-label col-1of10">ISBN</label>');
	detailBuffer.push('<label class="control-label col-1of3">'+$(data).parent().children("td[id='book_isbn']").text()+'</label>');
	detailBuffer.push('</div>');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<label class="control-label col-1of10">页数</label>');
	detailBuffer.push('<label class="control-label col-1of3">'+$(data).parent().children("td[id='book_page']").text()+'</label>');
	detailBuffer.push('</div>');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<label class="control-label col-1of10">价格</label>');
	detailBuffer.push('<label class="control-label col-1of3">'+$(data).parent().children("td[id='book_price']").text()+'</label>');
	detailBuffer.push('</div>');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<label class="control-label col-1of10">出版日期</label>');
	detailBuffer.push('<label class="control-label col-1of3">'+$(data).parent().children("td[id='book_pubDate']").text()+'</label>');
	detailBuffer.push('</div>');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<label class="control-label col-1of10">出版社</label>');
	detailBuffer.push('<label class="control-label col-1of3">'+$(data).parent().children("td[id='book_publisher']").text()+'</label>');
	detailBuffer.push('</div>');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<label class="control-label col-1of10">摘要</label>');
	detailBuffer.push('<label class="control-label col-1of3">'+$(data).parent().children("td[id='book_summary']").text()+'</label>');
	detailBuffer.push('</div>');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<label class="control-label col-1of10">主题</label>');
	detailBuffer.push('<label class="control-label col-1of3">'+$(data).parent().children("td[id='book_subject']").text()+'</label>');
	detailBuffer.push('</div>');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<label class="control-label col-1of10">索书号</label>');
	detailBuffer.push('<label class="control-label col-1of3">'+$(data).parent().children("td[id='book_callno']").text()+'</label>');
	detailBuffer.push('</div>');
	detailBuffer.push('<div class="form-group">');
	detailBuffer.push('<div class="col-1of5 col_inp">');
	detailBuffer.push('<img src="'+$(data).parent().children("td").eq(6).text()+'" style="width:185px;height:260px"/>');
	detailBuffer.push('</div>');
	detailBuffer.push('</div>');
	detailBuffer.push('</div>');
	detailBuffer.push('</div>');

//	alert($(data).parent().children("td").eq(15).text());
	bookDiv.html(detailBuffer.join(""));
	bookDiv.appendTo($("#main-content"));
	bookDiv.show();
	index_books_order++;
}
/**
 * 图书展示tab关闭
 * */
function index_deleteTab(liObj)
{
	var order_num = $(liObj).parent().attr("data-id");
	$(liObj).parent().prev().click();
	$("#book"+order_num).remove();
//	$("#frame" + order_num).remove();
	$(liObj).parent().remove();
}
/**
 * 图书展示详情
 * */
function showBook(data)
{
	index_addTab(data);
}
function shortTitle(str)
{
	if (str == null || str == undefined)
	{
		return "";
	}
	else
	{
		return (str.length > 6 ? str.substr(0,6) + "..." : str);
	}
	return str;
}
